package main.Q1201_1300;

public class Q1251_1260 {
    public static void main(String[] args) {
        System.out.println("Question1251：");
        System.out.println("Question1252：奇数值单元格的数目");
        System.out.println("Question1253：");
        System.out.println("Question1254：统计封闭岛屿的数目");
        System.out.println("Question1255：得分最高的单词集合");
        System.out.println("Question1256：");
        System.out.println("Question1257：");
        System.out.println("Question1258：");
        System.out.println("Question1259：");
        System.out.println("Question1260：");
    }
}

class Question1252 {
    public int oddCells(int m, int n, int[][] indices) {
        int result = 0;
        int[][] matrix = new int[m][n];
        for (int[] index : indices) {
            for (int i = 0; i < n; i++) matrix[index[0]][i]++;
            for (int i = 0; i < m; i++) matrix[i][index[1]]++;
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if ((matrix[i][j] & 1) != 0) result++;
            }
        }
        return result;
    }
}

class Question1254 {
    public boolean process(int[][] grid, int x, int y) {
        int h = grid.length, w = grid[0].length;
        if (x < 0 || y < 0 || x >= h || y >= w)
            return false;
        if (grid[x][y] == 1)
            return true;
        grid[x][y] = 1;
        boolean r1 = process(grid, x - 1, y);
        boolean r2 = process(grid, x, y + 1);
        boolean r3 = process(grid, x + 1, y);
        boolean r4 = process(grid, x, y - 1);
        return r1 && r2 && r3 && r4;
    }

    public int closedIsland(int[][] grid) {
        int count = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == 0 && process(grid, i, j)) count++;//为岛屿且封闭
            }
        }
        return count;
    }
}

class Question1255 {
    public int maxScoreWords(String[] words, char[] letters, int[] score) {
        int n = words.length, result = 0;
        int[] count = new int[26];
        for (char c : letters) {
            count[c - 'a']++;
        }
        for (int s = 1; s < (1 << n); s++) {
            int[] wordCount = new int[26]; // 统计子集 s 所有单词的字母数目
            for (int k = 0; k < n; k++) {
                if ((s & (1 << k)) == 0) { // words[k] 不在子集 s 中
                    continue;
                }
                for (int i = 0; i < words[k].length(); i++) {
                    char c = words[k].charAt(i);
                    wordCount[c - 'a']++;
                }
            }
            boolean ok = true; // 判断子集 s 是否合法
            int sum = 0; // 保存子集 s 的得分
            for (int i = 0; i < 26; i++) {
                sum += score[i] * wordCount[i];
                ok = ok && (wordCount[i] <= count[i]);
            }
            if (ok) {
                result = Math.max(result, sum);
            }
        }
        return result;
    }
}